
// mypages/mycoll/mycoll.js
const _ = wx.cloud.database().command
Page({

    /**
     * 页面的初始数据
     */
    data: {
        lilist: [],
        lilist1: '',
        lilist2: '',
        isMask: false,
        isAll: false,
        isEdit: false,
        dellist: [],
        one: 'one',
        two: 'two',
        activeNames: ['0'],
    },
    onChange(event) {
        this.setData({
            activeNames: event.detail,
        });
    },
    /** 
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let user = wx.getStorageSync('user');
        if (!user) {
            wx.showModal({
                title: '未登录',
                content: '确定现在去登录吗？',
                success: function (res) {
                    if (res.confirm) {
                        wx.switchTab({
                            url: '/mypages/my/my',
                        })
                    } else if (res.cancel) {
                        //console.log('点击取消了');
                        wx.switchTab({
                            url: '/mypages/my/my',
                        })

                    }
                }
            })
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.mylike();
        let that = this

    },
    //获取数据
    mylike: async function () {
        let that = this;
        let user = wx.getStorageSync('user');
        let arr = [];
        let phone = user.phone;
        await wx.cloud.database().collection('my_user').where({
            phone: phone
        }).get({
            success(res) {
                console.log(res);
                arr = res.data[0].collections
                that.autoplay(arr);
            },
            fail(res) {
                console.log(res);
            }
        })
    },
    autoplay: async function (Arr) {
        let that = this;
        let arr = Arr;
        let comarr = [];
        console.log("arr的值", arr);
        for (let i = 0; i < arr.length; i++) {
            await this.orderplay(arr[i], arr.length, comarr);
        }
        // console.log("结果",comarr);
    },
    //防止顺序错乱
    orderplay: function (arr, num, comarr) {
        let that = this;
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                wx.cloud.database().collection('my_share').where({
                    issue: _.elemMatch({ titid: _.eq(arr.titid) })
                }).get({
                    async success(res) {
                        console.log("获取成功", arr, num, res);
                        let arr2 = res.data[0].issue;
                        let arr3 = [{ name: res.data[0].name }, { userimg: res.data[0].userimg }]
                        for (let j = 0; j < arr2.length; j++) {
                            await that.orderplaytwo(arr, arr2[j], arr3, num, comarr)
                        }
                        // console.log("查看", i, comarr);
                    },
                    fail(res) {
                        console.log("获取失败", res);
                    }
                })
                resolve();
            }, 100)
        })

    },
    //内部for循环promise
    orderplaytwo: function (arr, arr2, arr3, num, comarr) {
        let that = this;
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                if (arr2.titid == arr.titid) {
                    arr2.name = arr3[0].name;
                    arr2.userimg = arr3[1].userimg;
                    comarr = comarr.concat(arr2)
                    console.log("arr3", comarr);

                    that.setData({
                        lilist: that.data.lilist.concat(comarr)
                    })
                    console.log("看lilist", that.data.lilist);
                }
                if (that.data.lilist.length == num) {
                    that.listplay()
                }
                resolve();
            }, 1)
        })

    },
    //分两列
    listplay: function () {
        let list = this.data.lilist
        let arr1 = []
        let arr2 = []
        console.log("是否结束", list);
        if (list.length > 1) {
            list.forEach((el, index) => {
                if ((index + 1) % 2 == 0) {
                    arr2 = arr2.concat(el)
                } else {
                    arr1 = arr1.concat(el)
                }
            })
            this.setData({
                lilist1: arr1,
                lilist2: arr2
            })
        } else if (list.length <= 1) {
            this.setData({
                lilist1: list
            })
        }

        console.log("arr1", arr1);
        console.log("arr2", arr2);
    },
    //点击编辑
    editplay: function () {
        if (this.data.isEdit) {
            this.setData({
                isEdit: false,
                isMask: false
            })
        }
        else {
            this.setData({
                isEdit: true,
                isMask: true
            })
        }
    },
    //是否全选
    allplay: function () {
        if (this.data.isAll) {
            this.setData({
                isAll: false
            })
            this.allchioce(false)
        } else {
            this.setData({
                isAll: true
            })
            this.allchioce(true);

        }
    },
    //全选操作
    allchioce: function (bol) {
        let list1 = this.data.lilist1;
        let list2 = this.data.lilist2;
        if (bol) {
            if (this.data.dellist.length != 0) {
                this.setData({
                    dellist: []
                })
                let list = this.data.dellist;
                for (let i = 0; i < list1.length; i++) {
                    list1[i].isDel = true
                    list = list.concat({ titid: list1[i].titid })
                }
                for (let j = 0; j < list2.length; j++) {
                    list2[j].isDel = true
                    list = list.concat({ titid: list2[j].titid })
                }
                this.setData({
                    lilist1: list1,
                    lilist2: list2,
                    dellist: list,
                })
            } else {
                let list = this.data.dellist;
                for (let i = 0; i < list1.length; i++) {
                    list1[i].isDel = true
                    list = list.concat({ titid: list1[i].titid })
                }
                for (let j = 0; j < list2.length; j++) {
                    list2[j].isDel = true
                    list = list.concat({ titid: list2[j].titid })
                }
                this.setData({
                    lilist1: list1,
                    lilist2: list2,
                    dellist: list,
                })
            }

        } else {
            for (let i = 0; i < list1.length; i++) {
                list1[i].isDel = false
            }
            for (let j = 0; j < list2.length; j++) {
                list2[j].isDel = false
            }
            this.setData({
                lilist1: list1,
                lilist2: list2,
                dellist: []
            })
        }

    },
    //选择删除
    delchioce: function (e) {
        let that = this;
        let id = e.currentTarget.dataset.id;
        let num = e.currentTarget.dataset.num;
        let thatlist = e.currentTarget.dataset.list;
        let list = this.data.dellist;
        let count = 0;
        console.log(thatlist);
        //标记
        if (thatlist == 'one') {
            let list1 = "lilist1[" + num + "].isDel"
            let bol = this.data.lilist1[num].isDel;
            if (bol) {
                console.log("看看这里");
                console.log(bol);
                that.setData({
                    [list1]: false
                })
            } else {
                that.setData({
                    [list1]: true
                })
            }
        } else if (thatlist == 'two') {
            let list2 = "lilist2[" + num + "].isDel"
            let bol = this.data.lilist2[num].isDel;
            if (bol) {
                console.log("看看这里");
                console.log(bol);
                that.setData({
                    [list2]: false
                })
            } else {
                that.setData({
                    [list2]: true
                })
            }
        }
        this.allshow();
        //收集标记文章放入数组中dellist
        if (list.length > 0) {
            for (let i = 0; i < list.length; i++) {
                if (list[i].titid == id) {
                    console.log("循环这里");
                    list.splice(i, 1)
                    this.setData({
                        dellist: list
                    })
                    count++;
                }
            }
        } else if (list.length <= 0) {
            console.log("执行了这里");
            this.setData({
                dellist: that.data.dellist.concat({ titid: id })
            })
            return
        }
        if (count == 0) {
            console.log("添加");
            this.setData({
                dellist: that.data.dellist.concat({ titid: id })
            })
        }
    },
    //全选图标隐显
    allshow: function () {
        let list1 = this.data.lilist1;
        let list2 = this.data.lilist2;
        let chiocenum = 0;
        for (let i = 0; i < list1.length; i++) {
            if (!list1[i].isDel) {
                chiocenum++;
            }
        }
        for (let j = 0; j < list2.length; j++) {
            if (!list2[j].isDel) {
                chiocenum++;
            }
        }
        if (chiocenum != 0) {
            this.setData({
                isAll: false
            })
        } else if (chiocenum == 0) {
            this.setData({
                isAll: true
            })
        }
    },
    //执行删除操作
    delplay: function () {
        let that = this
        let user = wx.getStorageSync('user');
        let arr = this.data.dellist
        console.log(this.data.dellist.length);
        console.log(this.data.dellist);
        if (arr.length == 0) {
            wx.showToast({
                title: '您还未选择',
                duration: 1000
            })
            return
        }
        for (let i = 0; i < arr.length; i++) {
            wx.cloud.database().collection('my_user').where({
                phone: user.phone
            }).update({
                data: {
                    collections: _.pull(
                        { titid: _.eq(arr[i].titid) }
                    )
                },
                success(res) {
                    console.log("删除成功");
                    if (i == arr.length - 1) {
                        that.setData({
                            lilist: []
                        })
                        that.mylike();
                    }
                },
                fail(res) {
                    console.log("删除失败", res);
                }

            })
        }

    },
    //跳转到具体文章
    urlnew: function (event) {
        // console.log(event);
        let id = event.currentTarget.dataset.id
        console.log(id);
        wx.navigateTo({
            url: '/mypages/news/news?titid=' + id,
            //   events: events,
            success: (result) => { },
            fail: (res) => { },
            complete: (res) => { },
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})